package com.ypf.survey.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.ypf.survey.dao.OptionDAO;
import com.ypf.survey.model.Option;

@Repository
public class OptionDAOImpl extends GenericDAOImpl<Option, Integer> implements OptionDAO {

	protected OptionDAOImpl() {
		super(Option.class.getName());
	}

	public Option getOptionById(int id) {
		DetachedCriteria criteria = DetachedCriteria.forEntityName(entityName);
		criteria.add(Restrictions.eq("id", id));
		return (Option) getHibernateTemplate().findByCriteria(criteria).get(0);
	}

	@SuppressWarnings("unchecked")
	public List<Option> getAllOptions() {
		DetachedCriteria criteria = DetachedCriteria.forEntityName(entityName);
		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		criteria.addOrder(Order.asc("question"));
		return getHibernateTemplate().findByCriteria(criteria);
	}
}
